﻿using JSONDB.helpers;
using OPCService.Job;
using OPCService.Service;
using OPCService.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using JSONItem = JSONDB.Item; //别名
using OpcItem = OPCService.Model.Item;

namespace OPCService
{
    public partial class OpcWrite : Form
    {
        private static string jsonconfig = AppDomain.CurrentDomain.BaseDirectory + @ConfigHelper.ReadConfig("jsondb");
        private static JsonDBHelper jsonDBHelper = new JsonDBHelper(jsonconfig);
        private static string opc800name = ConfigHelper.ReadConfig("Opc800DataCollection");
        private static string opc500name = ConfigHelper.ReadConfig("Opc500DataCollection");

        public OpcWrite()
        {
            InitializeComponent();
            initUI();
        }

        private void initUI()
        {
            //opc server
            cbOpcServer.Items.Clear();
            cbOpcServer.Items.Add(opc800name);
            cbOpcServer.Items.Add(opc500name);
            
            // 绑定事件
            cbOpcServer.SelectedIndexChanged += OpcServer_SelectedIndexChanged;
            cbTagName.SelectedIndexChanged += Tagname_SelectedIndexChanged;
            cbOpcServer.SelectedIndex = 0;
        }

        private void OpcServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (cb.SelectedIndex >= 0)
            {
                cbTagName.Items.Clear();
                cbTagName.SelectedIndex = -1;
                int total;
                List<string> tags = new List<string>();
                List<JSONItem> items = new List<JSONItem>();
                string selected = cb.SelectedItem.ToString();
                if (selected.Equals(opc800name))
                {
                    string[] tagsArr = { "一次风机给定", "二次风机给定" };
                    tags.AddRange(tagsArr);
                    items = jsonDBHelper.GetItems(opc800name, 200, 1, out total);
                }
                else if (selected.Equals(opc500name))
                {
                    string[] tagsArr = { "炉排1号逆推进到位信号", "炉排2号逆推进到位信号", "炉排3号逆推进到位信号", "炉排4号逆推进到位信号", "炉排1号顺推进到位信号", "炉排2号顺推进到位信号", "炉排3号顺推进到位信号", "炉排4号顺推进到位信号", "炉排1号推料器行程", "炉排2号推料器行程", "炉排3号推料器行程", "炉排4号推料器行程", "炉排1号顺推输出", "炉排2号顺推输出", "炉排3号顺推输出", "炉排4号顺推输出", "炉排1号逆推输出", "炉排2号逆推输出", "炉排3号逆推输出", "炉排4号逆推输出" };
                    tags.AddRange(tagsArr);
                    items = jsonDBHelper.GetItems(opc500name, 200, 1, out total);
                }

                foreach (JSONItem item in items)
                {
                    if (tags.Contains(item.name))
                    {
                        cbTagName.Items.Add(item.name);
                    }
                }
                cbTagName.SelectedIndex = 0;
            }
        }

        //读取值
        private void Tagname_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedOpcServer = cbOpcServer.SelectedItem.ToString();
            ComboBox cb = sender as ComboBox;
            if (cb.SelectedIndex >= 0)
            {
                string selected = cb.SelectedItem.ToString();
                string tagname = getTagByName(selected);
                object value = "";

                //MessageBox.Show("000000000000000000000" + tagname);
                if (selectedOpcServer.Equals(opc800name))
                {
                    value = Opc800DataCollection.ReadData(tagname);
                }
                else if (selectedOpcServer.Equals(opc500name))
                {
                    value = Opc500DataCollection.ReadData(tagname);
                }
                lblCurrVal.Text = value.ToString();
            }
        }

        //根据tag中文名获取英文名
        private string getTagByName(string cnname)
        {
            int total;
            string selectedTag = "";
            List<JSONItem> items = new List<JSONItem>();
            string selectedOpcServer = cbOpcServer.SelectedItem.ToString();
            if (selectedOpcServer.Equals(opc800name))
            {
                items = jsonDBHelper.GetItems(opc800name, 200, 1, out total);
            }
            else if (selectedOpcServer.Equals(opc500name))
            {
                items = jsonDBHelper.GetItems(opc500name, 200, 1, out total);
            }

            foreach (JSONItem item in items)
            {
                if (cnname.Equals(item.name))
                {
                    selectedTag = item.value;
                    break;
                }
            }

            return selectedTag;
        }

        //写入值
        private void btnWrite_Click(object sender, EventArgs e)
        {
            Task.Run(async () =>
            {
                try
                {
                    string selectedOpcServer = cbOpcServer.SelectedItem.ToString();
                    string selectedCnName = cbTagName.SelectedItem.ToString();
                    string tagname = getTagByName(selectedCnName);
                    string valueString = tbWriteVal.Text;
                    double.TryParse(valueString, out double value);
                    if (selectedOpcServer.Equals(opc800name))
                    {
                        await Opc800DataCollection.WriteData(tagname, value);
                    }
                    else if (selectedOpcServer.Equals(opc500name))
                    {
                        await Opc500DataCollection.WriteData(tagname, value);
                    }
                }
                catch (Exception ex)
                { }
            });
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            GrateLineService Line1Backward = new GrateLineService("炉排4号逆推进到位信号", "炉排4号逆推退到位信号", "炉排4号逆推前进指令", "炉排4号逆推后退指令");
        }
    }
}
